v0.4.0: Authentication & Authorization System#13
Merged
KeshavVarad merged 4 commits intomainfrom Nov 4, 2025
Merged
Conversation
Implement production-grade authentication and authorization system inspired by next-auth, adapted for the Model Context Protocol. Core Features: - Complete auth framework with AuthContext, AuthProvider, AuthResult - Permission and Role classes with wildcard support - 3 built-in auth providers (API Key, JWT, Session) - Full RBAC system with fine-grained permissions - 6 auth middleware decorators (sync/async variants) - Custom auth provider support Auth Providers: - APIKeyProvider: Pre-configured keys, custom validators, secure generation - JWTProvider: Token creation/verification, expiration handling (requires PyJWT) - SessionProvider: In-memory sessions, automatic cleanup, expiration RBAC System (nextmcp/auth/rbac.py): - Define and manage roles and permissions - Permission wildcards (admin:*, *) - Load configuration from dictionaries - Check and require permissions/roles - PermissionDeniedError exception Auth Middleware (nextmcp/auth/middleware.py): - @requires_auth / @requires_auth_async - @requires_role / @requires_role_async - @requires_permission / @requires_permission_async - Auth context injection as first parameter - Middleware stacking support Examples: - examples/auth_api_key/: API key auth with role-based access - examples/auth_jwt/: JWT auth with token generator utility - examples/auth_rbac/: Advanced RBAC with permission wildcards Tests (62 new, all passing): - test_auth_providers.py: 26 tests for all 3 providers - test_rbac.py: 36 tests for RBAC system - Total: 297/297 tests passing (100% backward compatible) Documentation: - Comprehensive README auth section (~400 lines) - Quick start examples for each provider - RBAC usage guide with wildcards - AuthContext, middleware, session management docs - Security best practices - 3 complete examples with READMEs Integration: - All 15 auth classes exported in nextmcp/__init__.py - Version bumped to 0.4.0 - CHANGELOG updated with detailed release notes - Updated comparison table and roadmap 100% backward compatible - all 235 original tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Fix linting issues in v0.4.0 auth implementation: Ruff fixes: - Remove unused imports (asyncio, typing.Optional, etc.) - Organize import statements - Prefix unused variables with underscore (_session1, etc.) - Add "from err" to ImportError raise in JWTProvider - Auto-fix import sorting across all files Black formatting: - Format nextmcp/auth/middleware.py - Format examples/auth_rbac/server.py - Format tests/test_auth_providers.py - Format tests/test_rbac.py All 297 tests still passing after linting fixes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Fix remaining linting issues caught by pre-commit hook: - examples/async_weather_bot/app.py: Remove unused batch_weather variable - examples/knowledge_base/app.py: Convert generator to set comprehension - examples/metrics_example/app.py: Rename unused loop variable to _i - examples/websocket_chat/client.py: Remove unused results variable - examples/websocket_chat/server.py: Convert generator to set comprehension All auto-fixed by ruff with --unsafe-fixes flag. All 297 tests still passing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add pre-commit hook to enforce code quality automatically: Features: - Runs ruff linting with auto-fix before each commit - Applies black formatting automatically - Runs full test suite to prevent breaking changes - Blocks commits that fail linting or tests - Auto-fixes applied but require manual review/staging Installation: - Hook stored in tracked `hooks/pre-commit` - Install script: `./scripts/install-hooks.sh` - Documented in README Development section Benefits: - Prevents linting failures in CI - Ensures consistent code formatting - Catches test failures before push - Saves review time by enforcing quality locally Hook runs automatically on `git commit`. Bypass with `git commit --no-verify` (not recommended). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements a complete authentication and authorization system for NextMCP v0.4.0, inspired by next-auth and adapted for the Model Context Protocol.
🔐 What's New
Core Auth Framework
AuthContext: Authentication context with user info, roles, and permissionsAuthProvider: Base class for implementing custom auth strategiesAuthResult: Authentication result with success/failure handlingPermission: Fine-grained permission model with wildcard support (admin:*,*)Role: Role class with permission collectionsBuilt-in Auth Providers
1. APIKeyProvider
2. JWTProvider
3. SessionProvider
RBAC System
RBACclass for role and permission managementPermissionDeniedErrorexceptionAuth Middleware
@requires_auth/@requires_auth_async- Require authentication@requires_role/@requires_role_async- Require specific roles@requires_permission/@requires_permission_async- Require specific permissions📚 Examples
Three complete examples with comprehensive READMEs:
examples/auth_api_key/- API key authentication with role-based access controlexamples/auth_jwt/- JWT token authentication with login endpoint and token generatorexamples/auth_rbac/- Advanced RBAC with fine-grained permissions and wildcards🧪 Tests
62 new tests, all passing:
test_auth_providers.py: 26 tests covering all 3 providerstest_rbac.py: 36 tests for RBAC systemTotal: 297/297 tests passing (235 original + 62 new)
📖 Documentation
🔄 Integration
nextmcp/__init__.pypyproject.tomlupdated💡 Usage Example
✅ Checklist
__init__.py🎯 Key Benefits
📊 Test Results
All original tests (235) + new auth tests (62) = 100% passing
🔒 Security
🤖 Generated with Claude Code